﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;

namespace CodeGenerate
{
    public class Utils
    {
        /// <summary>
        /// 获取当前数据库实体对象
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<Table> GetObjects(ObjectType type)
        {
            DataBaseService s = new DataBaseService();
            return s.GetObjects(type);
        }

        /// <summary>
        /// 获取当前数据列
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static List<Column> GetColumns(string tableName)
        {
            DataBaseService s = new DataBaseService();
            return s.GetColumns(tableName);
        }

        /// <summary>
        /// 获取存储过程参数
        /// </summary>
        /// <param name="procName"></param>
        /// <returns></returns>
        public static List<StoredProcedureParam> GetProcParams(string procName)
        {
            DataBaseService s = new DataBaseService();
            return s.GetParas(procName);
        }

        /// <summary>
        /// 获取当前主键列
        /// </summary>
        /// <param name="columns"></param>
        /// <returns></returns>
        public static Column GetPrimaryColumn(List<Column> columns)
        {
            var query = from v in columns
                        where v.IsPrimaryKey
                        select v;
            Column c= query.FirstOrDefault<Column>();
            return c == null ? new Column() : c;
        }

        #region Config
        /// <summary>
        /// 对象实体名称
        /// </summary>
        /// <returns></returns>
        public static string GetEntityName()
        {
            return GetAppSettingValue("EntityName");
        }

        private static string GetAppSettingValue(string key)
        {
            object value = ConfigurationManager.AppSettings[key];
            return value != null ? value.ToString() : string.Empty;
        }

        /// <summary>
        /// 获取业务实体名称
        /// </summary>
        /// <returns></returns>
        public static string GetBussinessName()
        {
            return GetAppSettingValue("BussinessName");
        }


        /// <summary>
        /// 版权公司
        /// </summary>
        public static string GetCopyrightCompany()
        {
            return GetAppSettingValue("CopyrightCompany");
        }

        /// <summary>
        /// 版权时间
        /// </summary>
        public static string GetCopyrightDate()
        {
            return GetAppSettingValue("CopyrightDate");
        }




        /// <summary>
        /// 公司网址
        /// </summary>
        public static string GetSiteUrl()
        {
            return GetAppSettingValue("SiteUrl");
        }

        /// <summary>
        /// 命名空间
        /// </summary>
        public static string GetRootNameSpace()
        {
            return GetAppSettingValue("RootNameSpace");
        }

        /// <summary>
        /// 获取代码生成器版本
        /// </summary>
        public static string GetVersion()
        {
            return GetAppSettingValue("Version");
        }

        /// <summary>
        /// 判断是否偶数行
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static bool IsEvenNumber(int number,int modNum)
        {
            return (number % modNum) == 0;
        }

        private static string[] entityNameRemove = GetAppSettingValue("EntityNameRemove").Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        /// <summary>
        /// 获取实体(DTO)名称
        /// </summary>
        public static string GetDTOName(string tabName)
        {
            string temp = tabName;
            if (entityNameRemove != null)
            {
                foreach (var str in entityNameRemove)
                {
                    temp = temp.Replace(str, string.Empty);
                }
            }
            //首字母大写
            return temp.Length > 0 ? temp.Remove(1).ToUpper() + temp.Substring(1): temp;
        }
        #endregion

        /// <summary>
        /// 当前用户
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUser()
        {
            return string.Format("*Author*");
        }

       

        /// <summary>
        /// 当前时间
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentDate()
        {
            return DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
        }

        /// <summary>
        /// 根据数据类型生成前缀
        /// </summary>
        /// <param name="type"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public static string GetProfix(DbType type, string sName)
        {
            string sp = "s";
            if (type == DbType.Boolean)
            {
                sp = "b";
            }
            else if (type == DbType.DateTime || type == DbType.Date)
            {
                sp = "dt";
            }
            else if (type == DbType.Decimal || type == DbType.Double)
            {
                sp = "d";
            }
            else if (type == DbType.Int32)
            {
                sp = "i";
            }
            else if (type == DbType.Int64)
            {
                sp = "l";
            }
            else if (type == DbType.Guid)
            {
                sp = "";
            }
            
            return string.Format("{0}{1}",sp,sName);
        }

        /// <summary>
        /// 获取存储过程 方法：参数名称
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static string GetProcParameterName(StoredProcedureParam para)
        {
            string sName = para.ParameterName.Replace("@","");
            return GetProfix(para.DbType, ConvertName(sName));
        }

        /// <summary>
        /// 生成参数输出方向
        /// </summary>
        /// <param name="para"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetProcParameterDirectionString(StoredProcedureParam para,int index)
        {
            StringBuilder strBuf = new StringBuilder();
            if (para.Direction != ParameterDirection.Input)
            {
                strBuf.AppendFormat("paras[{0}].Direction=ParameterDirection.{1};", index - 1, para.Direction);
                if (para.DbType == DbType.AnsiString || para.DbType == DbType.AnsiStringFixedLength || para.DbType == DbType.String || para.DbType == DbType.StringFixedLength)
                {
                    strBuf.AppendLine(Environment.NewLine);
                    strBuf.AppendFormat("paras[{0}].Size={1};", index - 1, para.Size);
                }
            }       

            return strBuf.ToString();
        }

        /// <summary>
        /// 转换第一个字母大写 其他小写
        /// table==> Table
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ConvertFirstUpper(string str)
        {           
            if (!Char.IsUpper(str, 0))
            {
                return String.Concat(str.Substring(0, 1).ToUpper(), str.Substring(1).ToLower());
            }
            return str;
        }

        /// <summary>
        /// 转换名称
        /// Te_Paper==> TePaper
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ConvertName(string str)
        {
            StringBuilder strBuf = new StringBuilder();
            string[] strArray = str.Split("_".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in strArray)
            {
                if (!string.IsNullOrEmpty(s))
                {
                    strBuf.AppendFormat("{0}", ConvertFirstUpper(s));
                }
            }
            return strBuf.ToString();
        }


        public static string GetType(DbType type)
        {
            string temp = type.ToString();
            switch (type)
            {
                case DbType.String:
                    break;
                case DbType.AnsiString:
                    temp = string.Format("string ");
                    break;
                case DbType.AnsiStringFixedLength:
                    temp = string.Format("string ");
                    break;
                case DbType.StringFixedLength:
                    temp = string.Format("string ");
                    break;
                case DbType.Binary:
                    temp = string.Format("byte[] ");
                    break;
                case DbType.Currency:
                    temp = string.Format("decimal ");
                    break;
                default:
                    temp = string.Format("{0} ", temp);
                    break;
            }
            return temp;
        }

        public static string GetTypeForNull(DbType type)
        {

            string temp = type.ToString();
            switch (type)
            {
                case DbType.String:
                    break;            
                case DbType.AnsiString:
                    temp = string.Format("string ");
                    break;
                case DbType.AnsiStringFixedLength:
                    temp = string.Format("string ");
                    break;
                case DbType.StringFixedLength:
                    temp = string.Format("string ");
                    break;
                default:
                    temp = string.Format("{0}? ", temp);
                    break;
            }
            return temp;
        }

        /// <summary>
        /// 生成CmdObjet类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetTypeForCmdObject(DbType type)
        {
            
            string temp = type.ToString();
            switch (type)
            {
                case DbType.String:
                    break;
                //case DbType.Guid:
                //    break;
                case DbType.AnsiString:
                    temp = string.Format("string ");
                    break;
                case DbType.AnsiStringFixedLength:
                    temp = string.Format("string ");
                    break;
                case DbType.StringFixedLength:
                    temp = string.Format("string ");
                    break;
                default:
                    temp = string.Format("{0}? ", temp);
                    break;
            }
            return temp;
        }
    }
}
